#include <iostream>
#include <string.h>
#include <stack>
#include <string>
using namespace std;




void _MergeSort(int* a, int* tmp, int begin, int end)
{
	if (end <= begin)
		return;

	int mid = (end + begin) / 2;
	// [begin, mid][mid+1, end]
	_MergeSort(a, tmp, begin, mid);
	_MergeSort(a, tmp, mid+1, end);

	int begin1 = begin, end1 = mid;
	int begin2 = mid+1, end2 = end;
	int index = begin;
	while (begin1 <= end1 && begin2 <= end2)
	{
		if (a[begin1] < a[begin2])
		{
			tmp[index++] = a[begin1++];
		}
		else		{
			tmp[index++] = a[begin2++];
		}
	}

	while (begin1 <= end1)
	{
		tmp[index++] = a[begin1++];
	}

	while (begin2 <= end2)
	{
		tmp[index++] = a[begin2++];
	}

	memcpy(a + begin, tmp + begin, (end - begin + 1)*sizeof(int));
}

void MergeSort(int* a, int n)
{
	int* tmp = (int*)malloc(sizeof(int) * n);
	if (tmp == NULL)
	{
		perror("malloc fail");
		return;
	}

	_MergeSort(a, tmp, 0, n - 1);

	free(tmp);
}

int GetMidi(int* a, int left, int right)
{
	int mid = (left + right) / 2;
	// left mid right
	if (a[left] < a[mid])
	{
		if (a[mid] < a[right])
		{
			return mid;
		}
		else if (a[left] > a[right])  // mid是最大值
		{
			return left;
		}
		else
		{
			return right;
		}
	}
	else // a[left] > a[mid]
	{
		if (a[mid] > a[right])
		{
			return mid;
		}
		else if (a[left] < a[right]) // mid是最小
		{
			return left;
		}
		else
		{
			return right;
		}
	}
}

void Swap(int* p1,int* p2){
  int tmp = *p1;
  *p1 = *p2;
  *p2 = tmp;
}

int PartSort(int* a,int left,int right){
  int midi = (left+right)/2;
  Swap(&a[midi],&a[left]);

  int keyi = left;

  while(left < right)
  {
    while(left < right && a[right] >= a[keyi]){
      --right;
    }

    while(left < right && a[left] <= a[keyi]){
      ++left;
    }

    Swap(&a[left],&a[right]);
  }

  Swap(&a[keyi],&a[left]);
  
  return left;
}

void QuickSortNonR(int* a,int begin,int end){
  stack<int> st;
  st.push(end);
  st.push(begin);

  while(!st.empty()){
    int left = st.top();
    st.pop();

    int right = st.top();
    st.pop();

    int keyi = PartSort(a,left,right);

    //[left,keyi-1] keyi [keyi+1,right]
    if(keyi+1 < right){
      st.push(right);
      st.push(keyi+1);
    }

    if(left < keyi-1){
      st.push(keyi-1);
      st.push(left);
    }

  }
}

void MergeSortNonR(int* a, int n)
{
	int* tmp = (int*)malloc(sizeof(int) * n);
	if (tmp == NULL)
	{
		perror("malloc fail");
		return;
	}

	int gap = 1;
	while (gap < n)
	{
		for (int i = 0; i < n; i += 2 * gap)
		{
			int begin1 = i, end1 = i + gap - 1;
			int begin2 = i + gap, end2 = i + 2 * gap - 1;
			// [begin1,end1] [begin2,end2] 归并

			if (begin2 >= n)
			{
				break;
			}

			if (end2 >= n)
			{
				end2 = n - 1;
			}

			int index = i;
			while (begin1 <= end1 && begin2 <= end2)
			{
				if (a[begin1] < a[begin2])
				{
					tmp[index++] = a[begin1++];
				}
				else
				{
					tmp[index++] = a[begin2++];
				}
			}

			while (begin1 <= end1)
			{
				tmp[index++] = a[begin1++];
			}

			while (begin2 <= end2)
			{
				tmp[index++] = a[begin2++];
			}

			// 拷贝回原数组
			memcpy(a + i, tmp + i, (end2-i+1) * sizeof(int));
		}

		printf("\n");

		gap *= 2;
	}

	free(tmp);
}

int main()
{

  int a[] = {6,1,2,7,9,3,4,5,10,8};
  //MergeSort(a,sizeof(a)/sizeof(int));

 // QuickSortNonR(a,0,sizeof(a)/sizeof(int)-1);
  MergeSortNonR(a,sizeof(a)/sizeof(int));
  for(int i =0; i<sizeof(a)/sizeof(int); i++){
    cout << a[i] << " ";
  }
  cout << endl;

  return 0;
}


